#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include <unistd.h>
#include <stdlib.h>

#include <string.h>

// ./send 20
int main(int argc, char const *argv[])
{
    if(argc != 2) return -1;
    key_t key = ftok("/", 100); // 获取IPC键
    int msgid = msgget(key, IPC_CREAT|0644); // 获取或创建消息队列
    if(msgid == -1){
        perror("msgget");
        return -1;
    }

    int data = atoi(argv[1]); // 将命令行中的数字转化为整数
    msgsnd(msgid, &data, 4, 0);
    printf("发送消息成功!\n");
    if(data == 0){
        sleep(5);
         int fd = open("a.txt", O_RDONLY);
         if(fd == -1) {
             perror("open");
             return -1;
         }

        long total = 0;
        read(fd, &total, sizeof(long));
        printf("total is %ld\n",  total);
        close(fd);
    }
  
    return 0;
}
